import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

public class RegisterFrame extends JFrame {

    private JLabel l_account, l_username, l_password1, l_password2, l_type, l_gender, l_city;
    private JTextField t_account, t_username;
    private JComboBox<String> c_type, c_city;
    private JRadioButton r_male, r_female;
    private ButtonGroup genderGroup;
    private JButton b_reset, b_login;
    private JPasswordField p_password1, p_password2;

    public RegisterFrame() {
        this.setTitle("电商购物平台-注册"); // 页面运行起来 左上角的标题
        this.setSize(450, 600); // 宽 * 高
        this.setLocation(700, 250); // 运行起来在屏幕中央的位置
        initComponents();
        setupLayout();
        setupListeners();
        this.setVisible(true); // 可见性
    }

    private void initComponents() {
        l_account = new JLabel("账号：", JLabel.CENTER);
        l_username = new JLabel("姓名：", JLabel.CENTER);
        l_password1 = new JLabel("密码：", JLabel.CENTER);
        l_password2 = new JLabel("确认密码：", JLabel.CENTER);
        l_type = new JLabel("用户类型：", JLabel.CENTER);
        l_gender = new JLabel("性别：", JLabel.CENTER);
        l_city = new JLabel("城市：", JLabel.CENTER);

        t_account = new JTextField();
        t_username = new JTextField();
        p_password1 = new JPasswordField();
        p_password2 = new JPasswordField();

        c_type = new JComboBox<String>();
        c_type.addItem("普通用户");
        c_type.addItem("管理员");

        c_city = new JComboBox<String>();
        c_city.addItem("太原");
        c_city.addItem("大同");
        c_city.addItem("北京");

        r_male = new JRadioButton("男");
        r_female = new JRadioButton("女");
        genderGroup = new ButtonGroup();
        genderGroup.add(r_male);
        genderGroup.add(r_female);

        b_reset = new JButton("重置");
        b_login = new JButton("注册");
    }

    private void setupLayout() {
        this.setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.insets = new java.awt.Insets(5, 5, 5, 5);

        // 信息输入面板
        JPanel p1 = new JPanel(new GridLayout(8, 2, 5, 5));
        JPanel genderPanel = new JPanel(new GridLayout(1, 2, 5, 5));
        genderPanel.add(r_male);
        genderPanel.add(r_female);

        p1.add(l_account);
        p1.add(t_account);
        p1.add(l_username);
        p1.add(t_username);
        p1.add(l_password1);
        p1.add(p_password1);
        p1.add(l_password2);
        p1.add(p_password2);
        p1.add(l_type);
        p1.add(c_type);
        p1.add(l_gender);
        p1.add(genderPanel);
        p1.add(l_city);
        p1.add(c_city);

        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.weightx = 1;
        gbc.weighty = 0.8;
        this.add(p1, gbc);

        // 按钮操作面板
        JPanel p2 = new JPanel(new GridLayout(1, 2, 5, 5));
        p2.add(b_reset);
        p2.add(b_login);

        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.weightx = 1;
        gbc.weighty = 0.2;
        this.add(p2, gbc);
    }

    private void setupListeners() {
        b_login.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String id = t_account.getText();
                String name = t_username.getText();
                String password = null;
                String password1 = new String(p_password1.getPassword());
                String password2 = new String(p_password2.getPassword());
                if (password1.equals(password2)) {
                    password = password1;
                } else {
                    JOptionPane.showMessageDialog(p_password1, "两次输入的密码不一致");
                    return;
                }
                char sex = r_male.isSelected()? '男' : '女';
                String city = (String) c_city.getSelectedItem();
                User user = new User(id, name, password, sex, city);
                new UserInfoFrame(user);
            }
        });
        b_reset.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 这里可以添加重置各输入框内容的逻辑
                t_account.setText("");
                t_username.setText("");
                p_password1.setText("");
                p_password2.setText("");
                genderGroup.clearSelection();
                c_city.setSelectedIndex(0);
            }
        });
    }
}
